﻿using System;
using System.Reflection;
using ESoft.DataAccess.EfCore.MySql;
using ESoft.Locking;
using ESoft.Logging;
using ESoft.Service;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using ESoft.RabbitMQ;
using ESoft.Redis;
using ESoft.Job;

namespace ESoft.All
{
    /// <summary>
    /// 初始化
    /// </summary>
    public class Init
    {
        /// <summary>
        /// 设置依赖
        /// </summary>
        /// <param name="services"></param>
        /// <param name="options"></param>
        /// <param name="assemblies"></param>
        public static void ConfigESoftService(IServiceCollection services,ESoftApplicationOptions options, params Assembly[] assemblies)
        {   
            if (options.DataAccessOption != null)
            {
                services.AddEfCoreMySql(options.DataAccessOption.ConnectionString, assemblies);
                services.AddService();
            }
            if (options.RedisOption != null)
            {
                services.AddRedis(options.RedisOption.ConnectStr);
                services.AddRedisLock();
            }
            if (options.RabbitMQOption != null)
            {
                services.AddRabbitMQ(assemblies);
            }
            if (options.NLogOption != null)
            {
                services.AddNLoging();
            }
            services.AddJob(assemblies);
        }

        /// <summary>
        /// 启动程序
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="assemblies"></param>
        public static void Configure(IServiceProvider serviceProvider, params Assembly[] assemblies)
        {
            var appOptions = serviceProvider.GetService<IOptions<ESoftApplicationOptions>>();
            var options = appOptions.Value;
            if (options.NLogOption != null)
            {
                serviceProvider.UseNLog(options.NLogOption.NLogConfigPath);
            }
            if (options.NLogOption != null)
            {
                serviceProvider.UseRabbitMQConsumer(assemblies);
            }
            serviceProvider.UseJob(assemblies);
        }
    }
}
